<?php
/* functions.new-finger.php */

/* show newest fingers */
function Finger_showNewest($howMany = 10) {
  $sql_cmd = "select `id`,`for`,`fingered` from fingers order by id desc limit 0,$howMany";
  $sql_query = mysql_query($sql_cmd) or die(mysql_error());
  if (mysql_num_rows($sql_query)>0) {
    while ($array = mysql_fetch_assoc($sql_query)) {
      $fingers = " finger";
      if ($array['fingered']>1) {
        $fingers = " fingers";
      }
      echo '<li><a href="'.Finger_link($array['for'], $array['id']).'">For '.substr($array['for'],0,100).'</a> <span>'.$array['fingered'].$fingers.'</span></li>';
    }
  }
}

/* show most fingered */
function Finger_showMostFingered($howMany = 10) {
  $sql_cmd = "select `id`,`for`,`fingered` from fingers order by fingered desc limit 0,$howMany";
  $sql_query = mysql_query($sql_cmd) or die(mysql_error());
  if (mysql_num_rows($sql_query)>0) {
    while ($array = mysql_fetch_assoc($sql_query)) {
      $fingers = " finger";
      if ($array['fingered']>1) {
        $fingers = " fingers";
      }
      echo '<li><a href="'.Finger_link($array['for'], $array['id']).'">For '.substr($array['for'],0,100).'</a> <span>'.$array['fingered'].$fingers.'</span></li>';
    }
  }
}

/* show all fingers order by newest or most fingered */
function Finger_showAllFingers($order) {
  $sql_cmd = "
  SELECT `f`.`id`, `f`.`for`, `f`.`fingered`, `c`.`comment` AS `why`, `c`.`type`, `u`.`name`
  FROM comments AS `c`
    LEFT JOIN `fingers` AS `f`
      ON `f`.`id` = `c`.`id_finger`
    LEFT JOIN `users` AS `u`
      ON `u`.`id` = `c`.`id_user`
  ";
  if ($order == 'newest') {
    $sql_cmd .= " order by `c`.timestamp desc";
  } else {
    $sql_cmd .= " GROUP BY `f`.id order by `f`.fingered desc";
  }
  $rows = Db::getInstance()->ExecuteS($sql_cmd);
  if (is_array($rows)) {
    foreach($rows as $array) {
      $fingers = " finger";
      if ($array['fingered']>1) {
        $fingers = " fingers";
      }
      if ($array['type'] == 1)
        $class = 'finger_up';
      elseif ($array['type'] == -1)
        $class = 'finger_down';
      else
        $class = '';
      echo '<li><span class="subtitle"><a class="'.$class.'" href="'.Finger_link($array['for'], $array['id']).'">For '.$array['for'].'</a> <strong>by '.$array['name'].'</strong> <span>'.$array['fingered'].$fingers.'</span></span>'.$array['why'].'</li>';
    }
  }
}

function Finger_get($id) {
  $sql_cmd = "select fingers.*, users.name
				from fingers 
					LEFT JOIN users ON users.id = fingers.id_user
				where fingers.id = $id";
  $sql_query = mysql_query($sql_cmd) or die(mysql_error());
  $row = mysql_fetch_assoc($sql_query);
  //echo $sql_cmd;
  return $row;
  //print_r($row);
}

/**
 * Create the URL to a finger
 * @param string $title The 'For' of the finger.
 * @param integer $id Id of the finger.
 * @return string Url to the finger.
 */
function Finger_link($title, $id) {
  $link = toSlug($title) . '-' . $id;
  return $link;
}

function Finger_fix() {
  $rows = Db::getInstance()->ExecuteS("select * from fingers order by id desc");
  foreach ($rows as $row) {
    echo "INSERT INTO `comments` (`id_finger`, `id_user`, `comment`, `type`, `timestamp`) VALUES
(".$row['id'].", ".$row['id_user'].", '".pSQL($row['why'])."', 1, ".$row['timestamp'].");";
    echo "<br>";
  }
}